<?php

namespace Modules\Order\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Order\Entities\BaseOrder as Order;

// use Maatwebsite\Excel\Facades\Excel;
// use Modules\Core\Entities\ImportCollection;

use Modules\Core\Services\ImportService;

use Validator;
use ThrowException;
use Storage;
use Cache;
use DB;

class DeliverController extends Controller
{
    public function batchDeliver(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $limitHeader = [
            '订单编号',
            '发货类型',
            '快递公司',
            '快递单号',
            '发货备注'
        ];
        $mapKey = [
            '订单编号' => 'number',
            '发货类型' => 'type',
            '快递公司' => 'express_name',
            '快递单号' => 'express_number',
            '发货备注' => 'remark'
        ];

        $rows = ImportService::mapArray($req->excel, $limitHeader, $mapKey);

        $rows = array_filter($rows, function ($item) {
            return $item['number'] && $item['type'];
        });

        $orderTotal = 0;
        $orderNotFoundRow = [];
        $orderStatusNotAllowRow = [];

        $numberList = [];
        foreach ($rows as $key => $row) {
            $order = Order::where('number', strVal($row['number']))->first();

            if (!$order) {
                array_push($orderNotFoundRow, $key + 1);
                continue;
            }

            if ($order->union_status !== 'deliver_pending') {
                array_push($orderStatusNotAllowRow, $key + 1);
                continue;
            }

            $orderTotal++;
        }

        $uuid = uniqid();

        Cache::put($uuid, $rows, 300);

        $rowTotal = count($rows);

        return [
            'row_total' => $rowTotal,
            'order_total' => $orderTotal,
            'not_found_rows' => $orderNotFoundRow,
            'not_deliver_pending_rows' => $orderStatusNotAllowRow, 
            'uuid' => $uuid
        ];
    }

    public function submit(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $uuid = $req->input('uuid');

        $rows = Cache::get($uuid);

        DB::beginTransaction();

        foreach ($rows as $row) {
            $number = strVal($row['number']);
            $inc = strVal($row['express_name']);
            $expressNumber = strVal($row['express_number']);
            $type = $row['type'] == '快递' ? 'express' : 'other';
            $remark = $row['remark'];

            $order = Order::status('deliver_pending')->where('number', $number)->first();

            if (!$order)
                continue;

            $order->deliver([
                'type' => $type,
                'express_name' => $inc,
                'express_number' => $expressNumber,
                'remark' => $remark
            ]);
        }

        // ThrowException::Conflict('中断');
        // return [];

        DB::commit();

        return [];
    }
}
